/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core.common;

import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.GspViewModelElement;
import com.inspur.edp.formserver.viewmodel.GspViewObject;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class VoActionUtil {
    private static HashMap<String, String> compMap = new HashMap<>();//VO元数据和UDT对应关系

    static {
        //传递 流程实例ID对应的属性名
        compMap.put("5b031a47-1aec-4a62-8e54-9ecb1f64be8e", "2e1beb7d-ad8f-4da3-a430-c8a7f2162135");//子表取消提交审批构件
        compMap.put("d96df086-5de6-40d3-8f03-67f67be1c0f3", "2e1beb7d-ad8f-4da3-a430-c8a7f2162135");//取消审批构件
        compMap.put("a42fe1c0-85b0-4067-b7b0-ee0f980cd620", "12be876c-368c-4262-88ab-4112688540b0");//获取子级父路径数据
        compMap.put("ccc7e0d8-2a21-4373-b583-a2f604ffaa91", "12be876c-368c-4262-88ab-4112688540b0");//父节点分级方式新增子级
        compMap.put("ea174384-6c8e-4de7-a10e-7794ace9c712", "12be876c-368c-4262-88ab-4112688540b0");//父节点分级方式新增同级
        compMap.put("15ae31f9-d2a1-439f-b14c-739b9267e66a", "12be876c-368c-4262-88ab-4112688540b0");//查询父路径树信息
        compMap.put("6830bf0d-9c70-4685-a312-2c8563c4cdbd", "dbfbe55d-ba65-4a7f-a9d4-4f664ec6ec68");//获取子级分级码数据
        compMap.put("af3d739f-0177-46cd-ac21-7ef7dabb2512", "dbfbe55d-ba65-4a7f-a9d4-4f664ec6ec68");//分级码分级方式新增子级
        compMap.put("541e13db-d2ab-42f9-8ed4-f883e731eba9", "dbfbe55d-ba65-4a7f-a9d4-4f664ec6ec68");//分级码分级方式新增同级
        compMap.put("95a8afeb-874f-4e0c-ad54-0e9729d6025a", "dbfbe55d-ba65-4a7f-a9d4-4f664ec6ec68");//查询分级码树信息

        //todo 传递 billstate对应属性名
        compMap.put("e2f80ac8-302d-4e02-b100-4ae9c58a968a", "a0b19650-0101-468f-ae3f-40c76c0f06b0");//提交审批构件
        compMap.put("f569b8a9-6d42-4c71-9a1f-d9d617819321", "a0b19650-0101-468f-ae3f-40c76c0f06b0");//子表提交审批构件
        compMap.put("1793fbf5-2fa3-48b5-a98a-aec1bd63f108", "a0b19650-0101-468f-ae3f-40c76c0f06b0");//根据入口单据提交审批
    }

    /**
     * 是否内置自定义动作
     *
     * @param componentId
     * @return
     */
    public static boolean isBuildInAction(String componentId) {
        return compMap.containsKey(componentId);
    }

    public static String getPropName(GspViewModel vo, String componentId, String nodeCode) {
        if (nodeCode == null || !vo.getNodesDic().containsKey(nodeCode) || vo.getMainObject().getCode().equalsIgnoreCase(nodeCode)) {
            List<GspViewModelElement> voMainObe = vo.getMainObject().getElements(n -> n.getIsUdt() && n.getUdtID().equals(compMap.get(componentId)));
            Objects.requireNonNull(voMainObe, "主节点未匹配到ID为：" + compMap.get(componentId) + "业务字段");
            return voMainObe.get(0).getLabelID();
        }
        GspViewObject childObj = vo.getNode(n -> n.getCode().equalsIgnoreCase(nodeCode));
        Objects.requireNonNull(childObj, "子对象" + nodeCode + "未匹配到ID为：" + compMap.get(componentId) + "业务字段");
        return childObj.getElements(n -> n.getIsUdt() && n.getUdtID().equals(compMap.get(componentId))).get(0).getLabelID();
    }
}
